import axios from '../../utils/axios'

const app = getApp()

Component({

  options: {
    styleIsolation: 'apply-shared',
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    shopp: Boolean,
    commodityInfo: {
      type: Object,
      value: null
    },
    classifyIndex: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    count: 1
  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 加入购物车
    plusCart: function (e) {

      let paramJson = {
        goods_id: this.properties.commodityInfo.goods_id,
        num: this.data.count,
        store_id: app.globalData.storeId
      }

      if (this.properties.commodityInfo.attach[this.data.classifyIndex].attach_id != -1) {
        paramJson['attach_id'] = this.properties.commodityInfo.attach[this.data.classifyIndex].attach_id
      }

      axios('/cloud/optGoods', paramJson).then(res => {
        if (res.data.resultcode == 1) {
          wx.showToast({
            title: '加入购物车成功！',
            icon: 'none',
            duration: 1200
          })
          let passJson = {
            attachName: this.properties.commodityInfo.attach[this.data.classifyIndex].name,
            count: this.data.count + '件'
          }
          // 像父组件传值
          this.triggerEvent('selectEvent', {
            passJson: passJson
          })
          this.setData({
            shopp: false
          })
        }
      })

    },

    // 立即购买
    shoppCommodity: function (e) {

      // 深复制
      let info = JSON.parse(JSON.stringify(this.properties.commodityInfo))

      info['num'] = this.data.count
      info['goods_name'] = info.name
      info['attach_id'] = info.attach[this.data.classifyIndex].attach_id
      info['attach_name'] = info.attach[this.data.classifyIndex].name
      info['showMoney'] = info.attach[this.data.classifyIndex].discount > 0.01 ? info.attach[this.data.classifyIndex].discount : info.attach[this.data.classifyIndex].price
      info['showCount'] = parseFloat(parseFloat(info.showMoney) * parseFloat(this.data.count)).toFixed(2)

      let itemJson = []
      itemJson.push(info)
      console.log(info)
      wx.setStorageSync('shoppList', itemJson)

      wx.navigateTo({
        url: '/pages/checkOrder/checkOrder?count=' + this.data.count,
      })

    },

    // 需要先登录
    getUserInfo: function (e) {

      // 当接口返回‘请登录’时，showLogin就变为true，然后调用授权
      if (e.detail.userInfo && app.globalData.showLogin) {
        app.globalData.showLogin = false
        const self = this
        app.signin(e.detail, function (res) {
          self.plusCart()
        })
      }

    },

    // 切换规格类型
    cutType: function (e) {
      this.setData({
        classifyIndex: e.currentTarget.dataset.index
      })
    },

    // 数量，加
    pulsCount: function () {
      this.setData({
        count: ++this.data.count
      })
    },

    // 数量，减
    subtractCount: function () {
      if (this.data.count > 1) {
        this.setData({
          count: --this.data.count
        })
      }
    },

    // 预览图片
    previewImg: function () {
      wx.previewImage({
        urls: [this.properties.commodityInfo.image],
        success: function (res) {
          console.log(res)
        }
      })
    },

    // 点击关闭
    closeModel: function () {
      this.setData({
        shopp: false
      })
    },

  }

})